﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections;

using System.Collections.ObjectModel;
using avlib_wrapper;

namespace dmm
{
    /// <summary>
    /// Interaction logic for RecordSelect.xaml
    /// </summary>
    /// 


    public partial class DlgRecordOption : Window
    {

        private class DeviceInfo
        {
            public int Id{get;set;}
            public string Name{get;set;}

            public DeviceInfo(int id, string name)
            {
                Id = id; Name = name;
            }
        }
        private Recorder recorder = null;
        //private AVRecordSetting setting = null;

        //does use select ok or cancel
        public bool Selection { get; set; }
        
        /*public AVRecordSetting RecordSetting
        {
            get { return setting; }
            set { setting = value; }
        }*/
        private ObservableCollection<Dimension> dims = new ObservableCollection<Dimension>();

        /*private ObservableCollection<DeviceInfo> vDevs = new ObservableCollection<DeviceInfo>();
        private ObservableCollection<DeviceInfo> aDevs = new ObservableCollection<DeviceInfo>();*/

        class Dimension
        {
            private UInt32 sz;
           
            public String StrDim
            {
                get
                {
                     UInt16 w = (UInt16)((sz >> 16) & 0xffff);
                     UInt16 h = (UInt16)(sz & 0xffff);
                    return String.Format("{0}x{1}", w, h);
                }
            }

            public UInt32 Dim
            {
                get { return sz; }
                set { sz = value;}
            }
        }

        public DlgRecordOption(Recorder corder)
        {
            Selection = false;
            recorder = corder;
          
            this.DataContext = this;
            InitializeComponent();
        }

        /*private void onAudioDevice(int id, string name)
        {
            aDevs.Add(new DeviceInfo(id, name));
            audio_src_ctrl.Items.Refresh();
        }

        private void onVideoDevice(int id, string name)
        {
            vDevs.Add(new DeviceInfo(id, name));
            video_src_ctrl.Items.Refresh();
        }*/
      
        private void Window_Loaded(object sender, EventArgs e)
        {
           
            //recorder.Init(this.onVideoDevice,this.onAudioDevice);
            
            video_src_ctrl.ItemsSource = recorder.VideoDevices;
            video_src_ctrl.DisplayMemberPath = "Name";
            video_src_ctrl.SelectedValuePath = "Id";
           

            audio_src_ctrl.ItemsSource = recorder.AudioDevices;
            audio_src_ctrl.DisplayMemberPath = "Name";
            audio_src_ctrl.SelectedValuePath = "Id";
            

            video_size_ctrl.ItemsSource = dims;
            video_size_ctrl.DisplayMemberPath = "StrDim";
            video_size_ctrl.SelectedValuePath = "Dim";

            if (recorder.VideoDevices.Count > 0) video_src_ctrl.SelectedIndex = 0;
            if (recorder.AudioDevices.Count > 0) audio_src_ctrl.SelectedIndex = 0;
            if (dims.Count > 0) video_size_ctrl.SelectedIndex = 0;

            rbDesktop.IsChecked = true;
            rbFullScreen.IsChecked = true;

            
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Selection = true;

            if (rbDesktop.IsChecked.HasValue && rbDesktop.IsChecked.Value)
            {
                recorder.Setting.VideoDevice = 0;
                if (rbFullScreen.IsChecked.HasValue && rbFullScreen.IsChecked.Value)
                {
                    recorder.Setting.Left = 0;
                    recorder.Setting.Top = 0;
                    recorder.Setting.Width = (int)System.Windows.SystemParameters.PrimaryScreenWidth;
                    recorder.Setting.Height = (int)System.Windows.SystemParameters.PrimaryScreenHeight;
                }
            }


            if(rbDirectShow.IsChecked.HasValue && rbDirectShow.IsChecked.Value){
                recorder.Setting.VideoDevice = (int)(video_src_ctrl.SelectedIndex + 1);

                UInt32 sz = (UInt32)video_size_ctrl.SelectedValue;
                UInt16 w = (UInt16)((sz >> 16) & 0xffff);
                UInt16 h = (UInt16)(sz & 0xffff);
                recorder.Setting.Width = w;
                recorder.Setting.Height = h;
                recorder.Setting.Left = recorder.Setting.Top = 0;
            }

           
            recorder.Setting.AudioDevice = (int)audio_src_ctrl.SelectedValue;
            recorder.Setting.FPS = (int)fps_ctrl.Value;

            Close();
        }

        private void video_src_ctrl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            dims.Clear();
            ArrayList arr = new ArrayList();
            recorder.QueryDimension((int)video_src_ctrl.SelectedIndex+1, arr);
            
            for (int i = 0; i < arr.Count; i++)
            {
                UInt32 sz = (UInt32)arr[i];
                Dimension d = new Dimension();
                d.Dim = sz;
                dims.Add(d);
            }

            video_size_ctrl.ItemsSource = dims;
            video_size_ctrl.Items.Refresh();
            video_size_ctrl.SelectedIndex = 0;
            
        }
        private void onRegionChanged(object sender, RoutedEventArgs e)
        {
            double w = System.Windows.SystemParameters.PrimaryScreenWidth;
            double h = System.Windows.SystemParameters.PrimaryScreenHeight;
            RegionSelector selector = sender as RegionSelector;
            recorder.Setting.Left =(int)(selector.Region.Left * w);
            recorder.Setting.Top = (int)(selector.Region.Top * h);
            recorder.Setting.Width = (int)(selector.Region.Width * w);
            recorder.Setting.Height = (int)(selector.Region.Height * h);

            //adjust to be 2-aligned 
            recorder.Setting.Width = (recorder.Setting.Width >> 1) * 2;
            recorder.Setting.Height = (recorder.Setting.Height >> 1) * 2;

        }

        private void onCloseRegionSelector(object sender, EventArgs e)
        {
            this.Show();
            this.Owner.Show();
        }
        private void onSelectRegion(object sender, RoutedEventArgs e)
        {
            RegionSelector selector = new RegionSelector();
            double w = System.Windows.SystemParameters.PrimaryScreenWidth;
            double h = System.Windows.SystemParameters.PrimaryScreenHeight;
            selector.set_bound(0, 0, w, h);

            double left = (double)recorder.Setting.Left / w;
            double top = (double)recorder.Setting.Top / h;
            double width = (double)recorder.Setting.Width / w;
            double height = (double)recorder.Setting.Height / h;

            selector.Region = new Rect(left, top, width, height);
            this.Hide();
            this.Owner.Hide();
            selector.RegionChanged += onRegionChanged;
            selector.Show();
            selector.Closed += onCloseRegionSelector;
        }

        private void rbDesktop_Checked(object sender, RoutedEventArgs e)
        {
            rbFullScreen.IsEnabled = true;
            rbSelectRegion.IsEnabled = true;
            btnSelectRegion.IsEnabled = true;

            video_src_ctrl.IsEnabled = false;
            video_size_ctrl.IsEnabled = false;
        }

    

        private void rbDirectShow_Checked(object sender, RoutedEventArgs e)
        {
            rbFullScreen.IsEnabled = false;
            rbSelectRegion.IsEnabled = false;
            btnSelectRegion.IsEnabled = false;

            video_src_ctrl.IsEnabled = true;
            video_size_ctrl.IsEnabled = true;
        }

      

      
    }
}
